@extends('admin.layouts.wap_simple')
@section('title', '开始认证小程序')
@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">
<div class="main-back">
    <div class="m-card">
        <div class="m-form-child m-col-12 m-xs-12" id="reg">
            <div class="m-form-item">
                <label><b>*</b>微信openid:</label>
                <div class="m-col-12">
                    <input type="text" class="m-input" id="openid" placeholder="请输入微信openid" maxlength="30" value="{{ empty($openid) ? '' : $openid }}">
                </div>
            </div>
            <div class="m-form-item">
                <label><b>*</b>小程序名称</label>
                <div class="m-col-12">
                    <input type="text" class="m-input" id="name" placeholder="请输入小程序" maxlength="11" value="">
                </div>
            </div>
            <div class="m-form-item">
                <div class="m-form-btn m-mr-1" onclick="submitAuth();">立即注册试用小程序</div>
            </div>
        </div>
        <div class="m-form-item" id="qrcode-view" style="display: none;">
            <div id="qrcode" style="width: 100%;display: flex;align-items: center;justify-content: center;margin: 15px 0 0;"></div>
            <p style="line-height: 1.3;padding: 35px 0 10px;text-align: center;width: 100%;">请管理员微信扫描二维码注册试用小程序</p>
        </div>
    </div>
</div>
<script src="https://wailiancn.oss-cn-beijing.aliyuncs.com/assets/js/v2/qrcode.min.js?versionId=CAEQIRiBgICI8_PA_RciIDE1ZmZiMmM2NmIyODRmMDE4Mzg0NWM5YTY5OTE5MDI0"></script>
<script type="text/javascript">
    $(function(){
        var openid = $("#openid").val();
        if(openid != ''){
            mviewToastr('info','检测到微信openid，已填充');
            return;
        } else {

        }
        openid = localStorage.getItem('openid') || '';
        if(openid != ''){
            $("#openid").val(openid);
            mviewToastr('info','检测到上次使用的微信openid，已填充');
            return;
        }
    })
    function submitAuth(){
        var openid = $("#openid").val();
        var name = $("#name").val();
        if(openid == ''){
            mviewToastr('error','请输入管理员的openid');
            return;
        }
        if(name == ''){
            mviewToastr('error','请输入小程序名称');
            return;
        }
        var _token = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            "url": "/admin/miniapps/fast_register_betaweapp",
            "type":"post",
            "data": {
                name:name,
                openid:openid
            },
            headers: {
                'X-CSRF-TOKEN': _token
            },
            success: function(res){
                console.log(res);
                if(res.status == 'success'){
                    var qrcode = new QRCode(document.getElementById("qrcode"), {
                        text: res.data,
                        width: 200,
                        height: 200,
                        colorDark : "#000000",
                        colorLight : "#ffffff",
                        correctLevel : QRCode.CorrectLevel.H
                    });
                    $("#reg").hide();
                    $("#qrcode-view").show();
                    localStorage.setItem('openid',openid);
                }else{
                    mviewToastr('error',res.msg);
                }
            },
            error: function(err){
                mviewToastr("error","网络异常,请刷新网页后重试!");
            }
        });
    }
</script>
@endsection
